using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using XProject.Entity;
using XProject.DAO;
using System.Configuration;
namespace XProject.GUI
{
    public partial class SayingManager : Form
    {
        SayingDAO daoSaying = new SayingDAO();
        public SayingManager()
        {
            InitializeComponent();
        }

        public void AddListItem(Saying saying)
        {
            ListViewItem item = new ListViewItem(saying.Content);
            item.SubItems.Add(saying.Owner);
            item.Tag = saying;
            lsvSaying.Items.Add(item);
        }
        public bool IsValidInput()
        {
            if (txtContent.Text.Trim() == "")
            {
                MessageBox.Show("CONTENT cannot be blank");
                return false;
            }
            return true;
        }
        public void Search()
        {
            IList<Saying> list = new List<Saying>();
            string cmd = txtSearch.Text.Trim().ToLower();
            if (cmd.StartsWith("type:"))
            {
                cmd = cmd.Replace("type:", "");
                if (cmbType.SelectedIndex > -1)
                {
                    Util.ListItem item = cmbType.SelectedItem as Util.ListItem;
                    if (item == null) return;
                    SayingType type = item.Tag as SayingType;
                    list = daoSaying.GetByTypeAndContent(type.TypeID, "%"+cmd+"%");                  
                }
            }
            else
            {
                list = daoSaying.GetByContent("%"+cmd+"%");
            }
            lsvSaying.Items.Clear();
            foreach (Saying saying in list) AddListItem(saying);
        }

        private void SayingManager_Load(object sender, EventArgs e)
        {
            SayingTypeDAO daoType = new SayingTypeDAO();
            //set icon
            //string icon = Application.StartupPath + "\\" + ConfigurationManager.AppSettings["icon"];
            //if (System.IO.File.Exists(icon)) { this.Icon = new Icon(icon); }
            //load data
            IList<SayingType> types = daoType.GetAll();
            IList<Saying> sayingList = daoSaying.GetTop();        
            
            if (types.Count > 0)
            {
                foreach (SayingType type in types)
                {
                    cmbType.Items.Add(new Util.ListItem(type.TypeName,type));
                }
                cmbType.SelectedIndex = 4;
            }
            foreach (Saying saying in sayingList)
            {
                AddListItem(saying);
            }
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            Saying saying = new Saying();
            saying.Content = txtContent.Text;
            saying.Owner = txtOwner.Text;
            saying.Note = txtNote.Text;
            Util.ListItem item=cmbType.SelectedItem as Util.ListItem;
            saying.Type = item.Tag as SayingType;
            if (daoSaying.Insert(saying) > 0)
            {
                //MessageBox.Show("Successed");
                lblMessage.Text = "Inserted successfully";
                AddListItem(saying);
                txtContent.Text = "";
                txtNote.Text = "";
                txtOwner.Text = "";
            }
            else MessageBox.Show(daoSaying.Message,"Error");
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            if (lsvSaying.SelectedItems.Count <= 0)
            {
                MessageBox.Show("Please select an item");
                return;
            }
            Saying saying = lsvSaying.SelectedItems[0].Tag as Saying;
            saying.Content = txtContent.Text;
            saying.Owner = txtOwner.Text;
            saying.Note = txtNote.Text;
            Util.ListItem item = cmbType.SelectedItem as Util.ListItem;
            saying.Type = item.Tag as SayingType;
            if (daoSaying.Update(saying) > 0)
            {
                //MessageBox.Show("Updated successfully");
                lblMessage.Text = "Updated successfully";
                lsvSaying.SelectedItems[0].SubItems[0].Text = saying.Content;
                lsvSaying.SelectedItems[0].SubItems[1].Text = saying.Owner;
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (lsvSaying.SelectedItems.Count <= 0)
            {
                MessageBox.Show("Please select an item");
                return;
            }
            if (MessageBox.Show("Are you sure want to delete selected item?", 
                "Confirm", MessageBoxButtons.YesNo, MessageBoxIcon.Question,
                MessageBoxDefaultButton.Button2) != DialogResult.Yes)
                return;
            Saying saying = lsvSaying.SelectedItems[0].Tag as Saying;
            if (daoSaying.Delete(saying) > 0)
            {
                MessageBox.Show("Deleted successfully");
                lsvSaying.Items.Remove(lsvSaying.SelectedItems[0]);
            }
            else MessageBox.Show(daoSaying.Message,"Error");
        }

        private void lsvSaying_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (lsvSaying.SelectedItems.Count <= 0) return;
            Saying saying = lsvSaying.SelectedItems[0].Tag as Saying;
            txtContent.Text = saying.Content;
            txtOwner.Text = saying.Owner;
            txtNote.Text = saying.Note;
            foreach (Util.ListItem item in cmbType.Items)
            {
                SayingType type = item.Tag as SayingType;
                if (type.TypeID == saying.Type.TypeID)
                {
                    cmbType.SelectedItem = item;
                    break;
                }
            }
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            Search();
        }

        private void txtSearch_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Return)
            {
                Search();
            }
        }
    }
}